package com.siruier.boss

import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.graphics.Typeface
import android.os.Build
import android.os.Process
import androidx.multidex.MultiDex
import com.ayl.deviceinfo.util.IMEIUtil
import com.didi.drouter.api.DRouter
import com.siruier.boss.api.TASK_API_SERVICE
import com.siruier.boss.api.core.ApiHelper
import com.siruier.boss.api.core.FirmException
import com.siruier.boss.api.core.service.IRouterService
import com.siruier.boss.api.core.service.RouterServiceBootstrap
import com.siruier.boss.api.core.toData
import com.siruier.boss.bean.ServiceConfig
import com.siruier.boss.common.MINI_PROGRAM_APP_ID
import com.siruier.boss.ui.AppCore
import com.siruier.boss.ui.interceptor.ConvertInterceptor
import com.siruier.boss.ui.utils.Logger
import com.siruier.boss.ui.utils.SPUtils
import com.siruier.boss.utils.MiitmdidHelper
import com.tencent.bugly.crashreport.CrashReport
import com.tencent.vasdolly.helper.ChannelReaderUtil
import com.umeng.commonsdk.UMConfigure
import com.umeng.socialize.PlatformConfig

class App : Application() {
    companion object {
        var OAID: String? = null
        lateinit var iconFontTypeface: Typeface
        private var app: App? = null
        private var init: Boolean = false
        fun init() {
            app?.init()
        }
    }

    override fun onCreate() {
        super.onCreate()
        app = this
        AppCore.init(this)
        if (SPUtils.getBoolean("sp_key_secret", false)) {
            init()
        }
    }

    private fun init() {
        val processName = getProcessName(this)
        if (packageName == processName && !init) {
            init = true
            val channel = ChannelReaderUtil.getChannel(applicationContext) ?: "app"
            Logger.e("channel==>${channel}")
            DRouter.init(this)
            MiitmdidHelper { oaid ->
                OAID = oaid
                Logger.e("OAID==>$OAID")
            }.getDeviceIds(this)
            Logger.isDebug = true
            SPUtils.put("baseUrl", ServiceConfig.getCurrentServiceConfig().apiUrl)
            ApiHelper.initHttp(
                ServiceConfig.getCurrentServiceConfig().apiUrl,
                channel,
                ConvertInterceptor(),
                Logger.isDebug
            )
            CrashReport.initCrashReport(applicationContext, "7f82bb7076", BuildConfig.DEBUG)
            iconFontTypeface = Typeface.createFromAsset(assets, "fonts/iconfont.ttf")
            // 61c84705e014255fcbc9abd2 604c315f6ee47d382b7f9ed4
            UMConfigure.preInit(this, "61e52dd1e0f9bb492bd3ea13", channel)
            UMConfigure.init(this, "61e52dd1e0f9bb492bd3ea13", channel, UMConfigure.DEVICE_TYPE_PHONE, "")
            // 微信设置
            PlatformConfig.setWeixin(MINI_PROGRAM_APP_ID, "aa02a4b2163b54b43b5afa2fa205e2e7")
            PlatformConfig.setWXFileProvider("${BuildConfig.APPLICATION_ID}.fileprovider")
            RouterServiceBootstrap.register(object : IRouterService {
                override suspend fun autoIbxLogin(): Boolean {
                    val imei = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
                        IMEIUtil.getIMEI(app)
                    } else {
                        OAID
                    }
                    if (imei == null) {
                        throw FirmException("请先做任务")
                    }
                    TASK_API_SERVICE.taskContent(imei).toData()
                    return true
                }
            })
        }
    }

    override fun attachBaseContext(context: Context?) {
        super.attachBaseContext(context)
        MultiDex.install(this)
    }

    private fun getProcessName(context: Context): String? {
        val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val runningApps = am.runningAppProcesses ?: return null
        for (proInfo in runningApps) {
            if (proInfo.pid == Process.myPid()) {
                if (proInfo.processName != null) {
                    return proInfo.processName
                }
            }
        }
        return null
    }
}